package com.hisense.hiioc.hismartplate.controller;

import com.alibaba.fastjson.JSONObject;
import com.hisense.hiioc.hismartplate.base.BaseController;
import com.hisense.hiioc.hismartplate.constants.URLConstant;
import com.hisense.hiioc.hismartplate.entity.dto.AppRetentionInfo;
import com.hisense.hiioc.hismartplate.entity.response.BaseRes;
import com.hisense.hiioc.hismartplate.entity.table.TestEntity;
import com.hisense.hiioc.hismartplate.service.EnvironmentService;
import com.hisense.hiioc.hismartplate.service.TestService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: almo
 * @Date: 2018/12/5 下午12:48
 * @Version 1.0
 */
@RestController
@RequestMapping(URLConstant.TestURLMapping.BASE_URL)
public class TestController extends BaseController<TestService> {

    @Autowired
    EnvironmentService environmentService;

    @Autowired
    RabbitTemplate rabbitTemplate;


    @RequestMapping(method = RequestMethod.POST, consumes = "application/json")
    public BaseRes<List<TestEntity>> index(@RequestBody TestEntity entity) {
        defaultService.saveTest(entity);

        final List<TestEntity> entities = defaultService.selectTestByName("almo");
        BaseRes<List<TestEntity>> res = new BaseRes<>();
        res.setErr_no(200);
        res.setSST(entities);
        return res;
    }

    @GetMapping("/testmq")
    public BaseRes<JSONObject> getEnvInfo(){
        AppRetentionInfo appRetentionInfo = new AppRetentionInfo();
        appRetentionInfo.setDevice("s");
        appRetentionInfo.setUrl("sdf");
        appRetentionInfo.setServiceName("高德地图");
        rabbitTemplate.convertAndSend("AppAccessInfo",appRetentionInfo );
        BaseRes<JSONObject> res = new BaseRes<>();
        res.setErr_no(HttpStatus.OK.value());
        res.setErr_msg("success");
        res.setSST(environmentService.getEnvironmentInfo());

        return res;
    }


}
